// pages/index/store_admin/index.js

import * as echarts from './echarts'
var option = {
  title: {
    text: '',
    left: 'center'
  },
  color: ["#37A2DA", "#67E0E3", "#9FE6B8"],
  legend: {
    
    top: 0,
    left: 'center',
    backgroundColor: 'white',
    z:0
  },
  grid: {
    left: '1%',
      right: '4%',
      bottom: '3%',
    containLabel: true
  },
  tooltip: {
    show: true,
    trigger: 'axis'
  },
  xAxis: {
    scale:true,
    type: 'category',
    boundaryGap: false,
    data: [],
    // show: false
  },
  yAxis: {
   
    x: 'center',
    type: 'value',
    splitLine: {
      lineStyle: {
        type: 'dashed'
      }
    }
    // show: false
  },
  series: [{
    name: '订单',
    type: 'line',
    smooth: true,
    data: []
  }, {
    name: '店铺',
    type: 'line',
    smooth: true,
    data: []
  },{
    name: '用户',
    type: 'line',
    smooth: true,
    data: []
  }]
};


const app = getApp();

function initChart(canvas, width, height, dpr) {
  const chart = echarts.init(canvas, null, {
    width: width,
    height: height,
    devicePixelRatio: dpr // new
  });
  canvas.setChart(chart);

  
  chart.setOption(option);
  return chart;
}





Page({

  data: {
    zen1:0,
    zen2:50,storelist:[],
    order:1,numberobj:{},bili:'',pricebili:'',shebeilist:[], ec: {
      onInit: initChart
    },orderlist:[],type:'',uid:'',xiajilist:[],count:'',page:1,isactive:0
  },

  onLoad: function (options) {
    this.getadddata()
    this.setData({type:options.type})
    this.setData({uid:wx.getStorageSync('uid')})
    if(options.type==2){
   this.getindexlist('')
   this.getechartlist()
   
   
   console.log(this.data.type);
    }else{
      this.getindexlist1()
      this.getshebeilist1('')
      this.getechartlist1()
      this.getstorelist1('')
      this.setData({type:options.type})
      console.log(this.data.type);
    }
   
  },
  onShow(){
    if(this.data.type==2){
     this.getshebeilist('')
     this.getstorelist('')
     this.getorderlist(1,'')}
     else{
       this.getindexlist1()
       this.getstorelist1('')
      this.getshebeilist1('')
      this.getorderlist1(1,'')
     }
  },
  
  navstore(){
    this.setData({
      order:2,
      page:1
    })
    if(this.data.type==2){
      this.getstorelist('')
    }else{
      this.getstorelist1('')
    }
  },
  navindex(){
    
    this.setData({
      order:1,
      page:1
    })
    if(this.data.type==2){
      this.getindexlist()
    }else{
      this.getindexlist1()
    }
 },
 navgoods(){
   this.setData({
     order:3,
     page:1
   })
   if(this.data.type==2){
    this.getshebeilist('')
    console.log(1);
    
  }else{
    this.getshebeilist1('')
    console.log(2);
    
  }
},
navorder(){
  if(this.data.order==4){
    return
  }
  this.setData({
    order:4
  })
  if(this.data.type==2){
    this.getorderlist(1,'')
  }else{
    this.getorderlist1(1,'')
  }
},
navxiaji(){
  this.setData({
    order:5
  })
  if(this.data.type==3){this.getxiajilist('')}
  
},
   //获取首页的数据
   getindexlist(){
    
     wx.request({
       url: 'https://zujieba.com/Api/storeadminindex/index.html',
       data:{uid:this.data.uid},
       success:(res)=>{
         console.log(res);
         let numberobj=res.data.data
         let bili=res.data.data.order_rate.toFixed(2)
         let pricebili=res.data.data.price_rate.toFixed(2)
         
         this.setData({
           numberobj,
           bili,
           pricebili
         })         
       }
     })
   },
   getechartlist(){
     wx.request({
       url: 'https://zujieba.com/Api/storeadminindex/census.html',
       data:{uid:this.data.uid},
       success:(res)=>{
         wx.stopPullDownRefresh()
         console.log(res);
          option.xAxis.data=res.data.categories
          option.series[0].data=res.data.order
          option.series[1].data=res.data.store
       }
     })
   },
   //共享商
   getechartlist1(){
    wx.request({
      url: 'https://zujieba.com/Api/shareradminindex/census.html',
      data:{uid:this.data.uid},
      success:(res)=>{
        wx.stopPullDownRefresh()
        console.log(res);
         option.xAxis.data=res.data.categories
         option.series[0].data=res.data.order
         option.series[1].data=res.data.store
         option.series[2].data=res.data.data
      }
    })
  },
   //获取店铺的数据 order==2的时候
    getstorelist(title){
      wx.request({
        url: 'https://zujieba.com/Api/storeadminstore/index.html',
        data:{uid:this.data.uid,title:title},
        success:(res)=>{
          wx.stopPullDownRefresh()
          console.log(res);
          let storelist=res.data.data
          this.setData({storelist})          
        }
      })
    },
    //添加店铺
    navtianjia(e){
      wx.navigateTo({
        url: '/pages/mine/store_admin/store_edit?type='+this.data.type
      })
    },
    //跳转到编辑店铺页面
    navedit1(e){
      wx.navigateTo({
        url: '/pages/mine/store_admin/store_add?id='+e.currentTarget.dataset.id+'&type='+this.data.type,
      })
    },
    //删除店铺
    remove(e){
       wx.showModal({
        showCancel:true,
         title:'确定删除店铺吗?',
         success:(res)=>{
           console.log(res);
           if(res.confirm==true){
             wx.request({
               url: 'https://zujieba.com/Api/storeadminstore/del_store.html',
               data:{id:e.currentTarget.dataset.id,uid:this.data.uid},
               success:(res1)=>{
                 
                 
                 console.log(res1);
                 if(res1.data.success==1){
                   wx.showToast({
                     title: '删除成功',
                   })
                   if(this.data.type==2){
                     this.getstorelist('')
                   }else{
                     this.getstorelist1('')
                   }
                 }else{
                   wx.showToast({
                     title: res1.data.tips,
                     icon:'none'
                   })
                 }
                 
               }
             })
           }
         }
       })
    },
    //获取设备的数据
     getshebeilist(title){
       
       
       wx.request({
         url: 'https://zujieba.com/Api/storeadmingoods/list.html',
         data:{uid:this.data.uid,title:title},
         success:(res)=>{
          wx.stopPullDownRefresh()
           console.log(res);
           this.setData({shebeilist:res.data.data})
           
         }
       })
     },
     remove1(e){
      wx.showModal({
        showCancel:true,
        title:'确定删除设备吗?',
        success:(res)=>{
          console.log(res);
          if(res.confirm==true){
            wx.request({
              url: 'https://zujieba.com/Api/storeadmingoods/del_goods.html',
              data:{id:e.currentTarget.dataset.id,uid:this.data.uid},
              success:(res1)=>{
                
                
                console.log(res1);
                if(res1.data.success==1){
                  wx.showToast({
                    title: '删除成功',
                  })
                  if(this.data.type==2){
                    this.getshebeilist('')
                  }else{
                    this.getshebeilist1('')
                  }
                }else{
                  wx.showToast({
                    title: res1.data.tips,
                    icon:'none'
                  })
                }
                
              }
            })
          }
        }
      })
     },
     //修改设备
     edit2(e){
        wx.navigateTo({
          url: '/pages/mine/store_admin/goods_edit?id='+e.currentTarget.dataset.id+"&type="+this.data.type,
        })
     },
     tianjia3(e){
       wx.navigateTo({
         url: '/pages/mine/store_admin/goods_add?type='+this.data.type,
       })
     },
     //order==4时候发起post
     getorderlist(page,title){
      wx.stopPullDownRefresh()
      wx.request({
        url: 'https://zujieba.com/Api/storeadminorder/list.html',
        data:{uid:this.data.uid,page:page,title:title},
        method:'POST',
        success:(res)=>{
          wx.stopPullDownRefresh()
          let orderlist
          console.log(res);
          if(page==1){ 
             this.setData({
              orderlist:res.data.data
          })}
          if(page!=1&&res.data.data.length!=0){
             orderlist=this.data.orderlist.concat(res.data.data)
             this.setData({orderlist})
          }
         
        
        }
      })
  },
     //查看订单详情
      navordermore(e){
        wx.navigateTo({
          url: '/pages/mine/store_admin/order_more?code='+e.currentTarget.dataset.code,
        })
      },
      onChangeAddress: function() {
            var _page = this;
          wx.chooseLocation({
            success: function(res) {
             _page.setData({
                 chooseAddress: res.name
               });
             },
              fail: function(err) {
                console.log(err)
             }
            });
         },
         //跳转到退款页面
         navtuikuan(e){
           wx.navigateTo({
             url: '/pages/mine/store_admin/refund?code='+e.currentTarget.dataset.code,
           })
         },
         //获取我的下级数据 order==5
         getxiajilist(title){
           
             wx.request({
               url: 'https://zujieba.com/Api/shareradminmember/list.html',
               data:{id:wx.getStorageSync('uid'),title:title},
               success:(res)=>{
                wx.stopPullDownRefresh()
                 console.log(res);
                 let count=res.data.count
                 let xiajilist=res.data.data
                 this.setData({
                   count,
                   xiajilist
                 })
                 
               }
             })
         },
         //解绑下级用户
         jiebang(e){
             wx.showModal({
              showCancel:true,
               title:'确认解除下级用户',
               success:(res)=>{
                 console.log(res);
                 if(res.confirm==true){
                   wx.request({
                     url: 'https://zujieba.com/Api/shareradminmember/del_member.html',
                     data:{uid:e.currentTarget.dataset.id,id:wx.getStorageSync('uid')},
                     success:(res1)=>{
                       console.log(res1);
                       if(res1.data.success==1){
                         wx.showToast({
                           title: '解绑成功',
                           duration:2000
                         })
                         this.getxiajilist('')
                       }
                       
                     }
                   })
                 }
                 
               }
               
             })
         },
           //获取首页的数据
   getindexlist1(){
    
    wx.request({
      url: 'https://zujieba.com/Api/shareradminindex/index.html',
      data:{id:this.data.uid},
      method:'post',
      success:(res)=>{
        wx.stopPullDownRefresh()
        console.log(res);
        let numberobj=res.data.data
        let bili=res.data.data.order_rate.toFixed(2)
        let pricebili=res.data.data.price_rate.toFixed(2)
        
        this.setData({
          numberobj,
          bili,
          pricebili
        })         
      }
    })
  },
   //获取店铺的数据 order==2的时候
   getstorelist1(title){
    wx.request({
      url: 'https://zujieba.com/Api/shareradminstore/list.html',
      data:{pid:this.data.uid,title:title},
      method:'POST',
      success:(res)=>{
        wx.stopPullDownRefresh()
        console.log(res);
        let storelist=res.data.data
        this.setData({storelist})          
      }
    })
  },
   //获取设备的数据
   getshebeilist1(title){
       
       
    wx.request({
      url: 'https://zujieba.com/Api/shareradmingoods/list.html',
      data:{uid:this.data.uid,title:title},
      method:'POST',
      success:(res)=>{
        wx.stopPullDownRefresh()
        console.log(res);
        this.setData({shebeilist:res.data.data})
        
      }
    })
  },
    //order==4时候发起post
    getorderlist1(page,title){
      wx.request({
        url: 'https://zujieba.com/Api/shareradminorder/list.html',
        data:{uid:this.data.uid,page:page,title:title},
        method:'POST',
        success:(res)=>{
          wx.stopPullDownRefresh()
          let orderlist
          console.log(res);
          if(page==1){ 
             this.setData({
              orderlist:res.data.data
          })}
          if(page!=1&&res.data.data.length!=0){
             orderlist=this.data.orderlist.concat(res.data.data)
             this.setData({orderlist})
          }
         
        
        }
      })
  },
//fahuo
lijifahuo(e){
     wx.navigateTo({
       url: '/pages/mine/store_admin/order_fahuo?code='+e.currentTarget.dataset.code,
     })
},
seewuliu(e){
     wx.navigateTo({
       url: '/pages/mine/store_admin/express?code='+e.currentTarget.dataset.code,
     })
},
tixian(){
  wx.navigateTo({
    url: '/pages/mine/tixian',
  })
},
 change9(e){
      if(this.data.type==2){this.getstorelist(e.detail.value)}
      else{this.getstorelist1(e.detail.value)}
     
 },
 
   change10(e){
     if(this.data.type==2){
      this.getshebeilist(e.detail.value)
     }else{
      this.getshebeilist1(e.detail.value)
     }
     
   },
   change11(e){
     if(this.data.type==2){
      this.getorderlist(1,e.detail.value)
     }else{
      this.getorderlist1(1,e.detail.value)
     }
   },
   change12(e){
     console.log(e);
     
      this.getxiajilist(e.detail.value)
   },
   onReachBottom(){
     if(this.data.order!=4){return}
    wx.showToast({
      title:'加载数据中...',
      icon:"loading"
    })
     let page=this.data.page+1
     this.setData({page:page})
      if(this.data.order==4){
         if(this.data.type==2){
           this.getorderlist(page,'')
         }else{
           this.getorderlist1(page,'')
         }
      }
   },
   //下拉刷新
   onPullDownRefresh(){
      if(this.data.type==2){
          if(this.data.order==1){
            this.getindexlist()
            this.getechartlist()
          }else if(this.data.order==2){
            this.getstorelist('')
          }else if(this.data.order==3){
            this.getshebeilist('')
          }else if(this.data.order==4){
            this.getorderlist(1,'')
          }

      }else{
        if(this.data.order==1){
          this.getindexlist1()
          this.getechartlist1()
        }else if(this.data.order==2){
          this.getstorelist1('')
        }else if(this.data.order==3){
          this.getshebeilist1('')
        }else if(this.data.order==4){
          this.getorderlist1(1,'')
        }else if(this.data.order==5){
          this.getxiajilist('')
        }
        
      }
   },
   navchongzhi(){
   this.getchongzhi()
     this.setData({
       order:6
     })

   },
   toggleisactive(e){
    
     console.log(e);
     
     this.setData({
       isactive:e.currentTarget.dataset.index
     })
   },
   getchongzhi(){
     console.log(11);
     
     wx.request({
       url: 'https://zujieba.com/Api/pack/index.html',
       data:{uid:wx.getStorageSync('uid')},
       method:'POST',
       success:(res)=>{
         console.log(res);
         let sql=res.data.data.sql
         sql.forEach((item)=>{
           item.price=parseInt(item.price)
         })
         this.setData({
           sql:res.data.data.sql,
           time:res.data.data.endtime
           
         })
         
       }
     })
   },
   addsubmit(){
     wx.request({
       url: 'https://zujieba.com/Api/pack/add_order.html',
       data:{uid:wx.getStorageSync('uid'),id:this.data.sql[this.data.isactive].id},
       method:'POST',
       success:(res)=>{
           console.log(res);
            this.order(res.data.data)
       }
     })},
     order(code){
      wx.request({
        url: 'https://zujieba.com/Api/pack/pay_order.html',
        data:{code},
        method:'POST',
        success:(res)=>{
            console.log(res);
              this.getpay(res.data.timeStamp,res.data.nonceStr,res.data.package,res.data.signType,res.data.paySign)
        }
      })
     },
     getpay(timeStamp,nonceStr,package1,signType,paySign){
       console.log(timeStamp);
       
      wx.requestPayment(
        {
        'timeStamp': timeStamp,
        'nonceStr': nonceStr,
        'package': package1,
        'signType': signType,
        'paySign': paySign,
       
       'success':(res)=>{
        
          console.log(res);
           
                wx.showToast({
                  title: '支付成功',
                  icon:'success',
                  duration:2000,
                  mask:true,
                  
                })
                this.getchongzhi()
             
               
  
               
               
               
          
        },
        fail:(res)=>{
         
        }
        
        })
    },
    // 奖励商户的数据
    getadddata(){
         wx.request({
           url: 'https://zujieba.com/Api/month/show.html',
           data:{uid:wx.getStorageSync('uid')},
           method:'POST',
           success:(res)=>{
             console.log(res);
             let storereward=[]
             let orderreward=[]
             let storerewardarr=[]
             let orderrewardarr=[]
             let data=res.data.data.activity
             data.forEach((item)=>{
               if(item.type==1){
                 storereward.push(item)
                 storerewardarr.push(item.people)
                 this.setData({
                   storereward
                  
                 })
               }
               if(item.type==2){
                orderreward.push(item)
                orderrewardarr.push(item.people)
                this.setData({
                  orderreward
                  
                })
               }
             })
             console.log(Math.max.apply(this,storerewardarr));
              let storebili=res.data.data.sql.people/Math.max.apply(this,storerewardarr)*100
              let orderbili=res.data.data.sql.new_month_order/Math.max.apply(this,orderrewardarr)*100
              this.setData({
                storebili,
                people:res.data.data.sql.people,
                new_month_order: res.data.data.sql.new_month_order,
                activity_type:res.data.data.config.activity_type,
                seniority:res.data.data.seniority,
                activity_addtime:res.data.data.config.activity_addtime,
                activity_endtime:res.data.data.config.activity_endtime,
                orderbili
              })
           }
         })
    },
    // 接受奖励任务
    aceeptstore(){
      wx.request({
        url: 'https://zujieba.com/Api/month/joins.html',
        data:{uid:wx.getStorageSync('uid'),type:1},
        method:'POST',
        success:(res)=>{
          console.log(res);
          this.getadddata()
          
        }
      })
    },
     // 接受奖励任务
     aceeptorder(){
      wx.request({
        url: 'https://zujieba.com/Api/month/joins.html',
        data:{uid:wx.getStorageSync('uid'),type:2},
        method:'POST',
        success:(res)=>{
          console.log(res);
          this.getadddata()
        }
      })
    }
  


})